#include "solar/trunk/NScopLabel.h"
#include "tools/Pyscript.h"

NCropLabel::NCropLabel(QString Imgcurtain, QWidget *parent):penwidth(2){
    if (!Imgcurtain.isNull()) setPixmap(Imgcurtain);
    RdoMap_Integer["tracking-start-x"] = width() / 2;
    RdoMap_Integer["tracking-start-y"] = height() / 2;
    RdoMap_Integer["Width"] = width();
    RdoMap_Integer["Height"] = height();
    RdoMap_Bool["focus-enter-state"] = false;
    RdoMap_Bool["tracking-enable"] = false;
    RdoMap_Bool["supreme-authority-pass"] = false;
    RdoMap_Bool["graph-geometry-change"] = false;
    setParent(parent);
}

void NCropLabel::setCoord(int x, int y, int w, int h){
    int hfrw = w / 2;
    int hflh = h / 2;
    RdoMap_Integer["round_width"] = w;
    RdoMap_Integer["round_height"] = h;
    if (RdoMap_Integer["tracking-start-x"] > hfrw)
    {
        RdoMap_Integer["tracking-start-x"] = x + hfrw;
    }
    else
    {
        RdoMap_Integer["tracking-start-x"] = -x + hfrw;
    }
    if (RdoMap_Integer["tracking-start-y"] > hflh)
    {
        RdoMap_Integer["tracking-start-y"] = y + hflh;
    }
    else
    {
        RdoMap_Integer["tracking-start-y"] = -y + hflh;
    }
    RdoMap_Bool["supreme-authority-pass"] = true;
    update();
}

void NCropLabel::setCoord(QRect rect)
{
    setCoord(rect.x(), rect.y(), rect.width(), rect.height());
}

void NCropLabel::setFocusEnterState(bool Enable){
    RdoMap_Bool["focus-enter-state"] = Enable;
    update();
}

void NCropLabel::enterEvent(QEnterEvent*){
    RdoMap_Bool["focus-enter-state"] = true;
    emit msev_etr(true);
    update();
}

void NCropLabel::leaveEvent(QEvent*){
    RdoMap_Bool["focus-enter-state"] = false;
    emit msev_etr(false);
    update();
}

void NCropLabel::resizeEvent(QResizeEvent *event){
    QPixmap maskPixmap = property("back-pixmap").value<QPixmap>();
    PPScriptExec::pyScript_PixmapVariant(size(), maskPixmap, QSize(-1, height()));
    setProperty("back-pixmap", maskPixmap);
    setFixedWidth(maskPixmap.width());
    if (!RdoMap_Bool["supreme-authority-pass"])
    {
        if (width() < height())
        {
            RdoMap_Integer["round_width"] = event->size().width() - penwidth;
            RdoMap_Integer["round_height"] = event->size().width() - penwidth;
        }
        else
        {
            RdoMap_Integer["round_width"] = event->size().height() - penwidth;
            RdoMap_Integer["round_height"] = event->size().height() - penwidth;
        }
        RdoMap_Integer["tracking-start-x"] = RdoMap_Integer["round_width"] / 2;
        RdoMap_Integer["tracking-start-y"] = RdoMap_Integer["round_height"] / 2;
    }
    QWidget::resizeEvent(event);
}

void NCropLabel::setPixmap(QString curtain){
    QPixmap maskPixmap;
    PPScriptExec::pyScript_PixmapVariant(size(), maskPixmap, QSize(), false, curtain);
    PPScriptExec::pyScript_PixmapVariant(size(), maskPixmap, QSize(-1, height()));
    setProperty("back-pixmap", maskPixmap);
    setFixedWidth(maskPixmap.width());
    update();
}

void NCropLabel::mousePressEvent(QMouseEvent *Event){
    if (Event->button() == Qt::LeftButton)
    {
        hasMouseTracking() ? setMouseTracking(false) : setMouseTracking(true);
    } else if (Event->button() == Qt::RightButton)
    {
        if (RdoMap_Bool["graph-geometry-change"])
        {
            RdoMap_Bool["graph-geometry-change"] = false;
        } else  {
            RdoMap_Bool["graph-geometry-change"] = true;
        }
    }
    update();
}

void NCropLabel::mouseMoveEvent(QMouseEvent *event){
    int round_redius = RdoMap_Integer["round_width"] / 2;
    int left_border = event->position().x() - round_redius; // 兜底左边距
    int right_border = width() - round_redius; // 最大右边距
    int top_border = event->position().y() - round_redius; // 兜底上边距
    int buttom_border = height() - round_redius;  // 最大底边距
    if (hasMouseTracking() && !RdoMap_Bool["supreme-authority-pass"]) //点击鼠标切换追踪状态weifalse后，停止更新图形中心坐标
    {
        // 如果左边距有空余(至少贴在左边框)，并且当前坐标X值没有超过最大右边距，则认为合理的，正常赋值
        if (left_border >= 0 && event->position().x() <= right_border)
        {
            RdoMap_Integer["tracking-start-x"] = event->position().x();
        } else {
            // 如果左边距小于0(圆边已经超出了左边框)，则将坐标定为圆半径，保持贴着左边框，避免越界
            if (left_border < 0)
            {
                RdoMap_Integer["tracking-start-x"] = round_redius + penwidth;
            }
            // 如果右边距大于最大右边距(圆边已经超出了右边框)，则将坐标定为圆半径，保持贴着右边框，避免越界
            if (event->position().x() > right_border)
            {
                RdoMap_Integer["tracking-start-x"] = right_border - penwidth;
            }
        }
        // 如果上边距有空余(至少贴在上边框)，并且当前坐标Y值没有超过最大底边距，则认为合理的，正常赋值
        if (top_border >= 0 && event->position().y() <= buttom_border)
        {
            RdoMap_Integer["tracking-start-y"] = event->position().y();
        } else {
            // 如果上边距小于0(圆边已经超出了上边框)，则将坐标定为圆半径，保持贴着上边框，避免越界
            if (top_border < 0)
            {
                RdoMap_Integer["tracking-start-y"] = round_redius + penwidth;
            }
            // 如果底边距大于最大底边距(圆边已经超出了底边框)，则将坐标定为圆半径，保持贴着底边框，避免越界
            if (event->position().y() > buttom_border)
            {
                RdoMap_Integer["tracking-start-y"] = buttom_border - penwidth;
            }
        }
        update();
    }
}

void NCropLabel::wheelEvent(QWheelEvent *Event){
    if (Event->angleDelta().y() > 0)
    {
        if (height() < width())
        {
            if (RdoMap_Integer["round_width"] + penwidth < height())
            {
                RdoMap_Integer["round_width"] += 2;
            }
            if (RdoMap_Integer["round_height"] + penwidth < height())
            {
                RdoMap_Integer["round_height"] += 2;
            }
        } else {
            if (RdoMap_Integer["round_width"] + penwidth < width())
            {
                RdoMap_Integer["round_width"] += 2;
            }
            if (RdoMap_Integer["round_height"] + penwidth < width())
            {
                RdoMap_Integer["round_height"] += 2;
            }
        }
    } else {
        if (RdoMap_Integer["round_width"] > 30)
        {
            RdoMap_Integer["round_width"] -= 2;
        }
        if (RdoMap_Integer["round_height"] > 30)
        {
            RdoMap_Integer["round_height"] -= 2;
        }
    }
    update();
}

void NCropLabel::paintEvent(QPaintEvent *){
    QPainter CaptainBrush(this);
    CaptainBrush.setRenderHints(CaptainBrush.SmoothPixmapTransform |
                                CaptainBrush.Antialiasing);
    CaptainBrush.setPen(Qt::NoPen);
    QPixmap maskPixmap = property("back-pixmap").value<QPixmap>();
    CaptainBrush.drawTiledPixmap(CaptainBrush.viewport(), maskPixmap);
    if (RdoMap_Bool["focus-enter-state"])
    {
        QPen round_pen = QPen(QColor(qRgb(151, 187, 212)));
        round_pen.setWidth(penwidth);
        CaptainBrush.setPen(round_pen);
        int focus_centrax = abs(RdoMap_Integer["tracking-start-x"] - (RdoMap_Integer["round_width"] / 2)); // 图形x轴起点，圆心x减半径
        int focus_centray = abs(RdoMap_Integer["tracking-start-y"] - (RdoMap_Integer["round_height"] / 2)); // 图形y轴起点，圆心y减半径
        //不限制图形绘画。坐标更迭由追踪状态控制
        QRect drawroundrect(focus_centrax,
                            focus_centray,
                            RdoMap_Integer["round_width"],
                            RdoMap_Integer["round_height"]);
        int radiusx;
        int radiusy;
        if (RdoMap_Bool["graph-geometry-change"])
        {
            radiusx = 0;
            radiusy = 0;

        } else {
            radiusx = RdoMap_Integer["round_width"] / 2;
            radiusy = RdoMap_Integer["round_height"] / 2;
        }
        CaptainBrush.drawRoundedRect(drawroundrect,
                                     radiusx,
                                     radiusy,
                                     Qt::AbsoluteSize);
        emit clipping_send({focus_centrax,
                            focus_centray,
                            RdoMap_Integer["round_width"],
                            RdoMap_Integer["round_height"]});
        emit clipsize_send({width(), height()});
        if (RdoMap_Bool["supreme-authority-pass"]) RdoMap_Bool["supreme-authority-pass"] = false;
    }
}

NCropLabel::~NCropLabel(){
    RdoMap_Integer.clear();
    RdoMap_String.clear();
    RdoMap_Bool.clear();
}
